.TH "cache_handler: Maintains a cache of data for use by lower level handlers." 3 "30 Jun 2005" "Version 5.2.1.rc3" "net-snmp" \" -*- nroff -*-
.ad l
.nh
.SH NAME
cache_handler: Maintains a cache of data for use by lower level handlers. \- This helper checks to see whether the data has been loaded 'recently' (according to the timeout for that particular cache) and calls the registered 'load_cache' routine if necessary.  

.PP
.SS "Functions"

.in +1c
.ti -1c
.RI "netsnmp_cache * \fBnetsnmp_cache_get_head\fP (void)"
.br
.RI "\fIget cache head \fP"
.ti -1c
.RI "netsnmp_cache * \fBnetsnmp_cache_find_by_oid\fP (oid *rootoid, int rootoid_len)"
.br
.RI "\fIfind existing cache \fP"
.ti -1c
.RI "netsnmp_cache * \fBnetsnmp_cache_create\fP (int timeout, NetsnmpCacheLoad *load_hook, NetsnmpCacheFree *free_hook, oid *rootoid, int rootoid_len)"
.br
.RI "\fIreturns a cache \fP"
.ti -1c
.RI "unsigned int \fBnetsnmp_cache_timer_start\fP (netsnmp_cache *cache)"
.br
.RI "\fIstarts the recurring cache_load callback \fP"
.ti -1c
.RI "void \fBnetsnmp_cache_timer_stop\fP (netsnmp_cache *cache)"
.br
.RI "\fIstops the recurring cache_load callback \fP"
.ti -1c
.RI "\fBnetsnmp_mib_handler\fP * \fBnetsnmp_cache_handler_get\fP (netsnmp_cache *cache)"
.br
.RI "\fIreturns a cache handler that can be injected into a given handler chain. \fP"
.ti -1c
.RI "\fBnetsnmp_mib_handler\fP * \fBnetsnmp_get_cache_handler\fP (int timeout, NetsnmpCacheLoad *load_hook, NetsnmpCacheFree *free_hook, oid *rootoid, int rootoid_len)"
.br
.RI "\fIreturns a cache handler that can be injected into a given handler chain. \fP"
.ti -1c
.RI "int \fBnetsnmp_cache_handler_register\fP (\fBnetsnmp_handler_registration\fP *reginfo, netsnmp_cache *cache)"
.br
.RI "\fIfunctionally the same as calling \fBnetsnmp_register_handler()\fP but also injects a cache handler at the same time for you. \fP"
.ti -1c
.RI "int \fBnetsnmp_register_cache_handler\fP (\fBnetsnmp_handler_registration\fP *reginfo, int timeout, NetsnmpCacheLoad *load_hook, NetsnmpCacheFree *free_hook)"
.br
.RI "\fIfunctionally the same as calling \fBnetsnmp_register_handler()\fP but also injects a cache handler at the same time for you. \fP"
.ti -1c
.RI "NETSNMP_STATIC_INLINE char * \fB_build_cache_name\fP (const char *name)"
.br
.ti -1c
.RI "void \fBnetsnmp_cache_reqinfo_insert\fP (netsnmp_cache *cache, \fBnetsnmp_agent_request_info\fP *reqinfo, const char *name)"
.br
.RI "\fIInsert the cache information for a given request (PDU). \fP"
.ti -1c
.RI "netsnmp_cache * \fBnetsnmp_cache_reqinfo_extract\fP (\fBnetsnmp_agent_request_info\fP *reqinfo, const char *name)"
.br
.RI "\fIExtract the cache information for a given request (PDU). \fP"
.ti -1c
.RI "netsnmp_cache * \fBnetsnmp_extract_cache_info\fP (\fBnetsnmp_agent_request_info\fP *reqinfo)"
.br
.RI "\fIExtract the cache information for a given request (PDU). \fP"
.ti -1c
.RI "int \fBnetsnmp_cache_check_expired\fP (netsnmp_cache *cache)"
.br
.RI "\fICheck if the cache timeout has passed. \fP"
.ti -1c
.RI "int \fBnetsnmp_cache_check_and_reload\fP (netsnmp_cache *cache)"
.br
.RI "\fIReload the cache if required. \fP"
.ti -1c
.RI "int \fBnetsnmp_cache_is_valid\fP (\fBnetsnmp_agent_request_info\fP *reqinfo, const char *name)"
.br
.RI "\fIIs the cache valid for a given request? \fP"
.ti -1c
.RI "int \fBnetsnmp_is_cache_valid\fP (\fBnetsnmp_agent_request_info\fP *reqinfo)"
.br
.RI "\fIfor backwards compat \fP"
.ti -1c
.RI "int \fBnetsnmp_cache_helper_handler\fP (\fBnetsnmp_mib_handler\fP *handler, \fBnetsnmp_handler_registration\fP *reginfo, \fBnetsnmp_agent_request_info\fP *reqinfo, \fBnetsnmp_request_info\fP *requests)"
.br
.RI "\fIImplements the cache handler. \fP"
.ti -1c
.RI "void \fBrelease_cached_resources\fP (unsigned int regNo, void *clientargs)"
.br
.RI "\fIrun regularly to automatically release cached resources. \fP"
.in -1c
.SH "Detailed Description"
.PP 
This helper checks to see whether the data has been loaded 'recently' (according to the timeout for that particular cache) and calls the registered 'load_cache' routine if necessary. 
.PP
The lower handlers can then work with this local cached data.
.PP
A timeout value of -1 will cause \fBnetsnmp_cache_check_expired()\fP to always return true, and thus the cache will be reloaded for every request.
.PP
To minimze resource use by the agent, a periodic callback checks for expired caches, and will call the free_cache function for any expired cache.
.PP
The load_cache route should return a negative number if the cache was not successfully loaded. 0 or any positive number indicates successs.
.PP
Several flags can be set to affect the operations on the cache.
.PP
If NETSNMP_CACHE_DONT_INVALIDATE_ON_SET is set, the free_cache method will not be called after a set request has processed. It is assumed that the lower mib handler using the cache has maintained cache consistency.
.PP
If NETSNMP_CACHE_DONT_FREE_BEFORE_LOAD is set, the free_cache method will not be called before the load_cache method is called. It is assumed that the load_cache routine will properly deal with being called with a valid cache.
.PP
If NETSNMP_CACHE_DONT_FREE_EXPIRED is set, the free_cache method will not be called with the cache expires. The expired flag will be set, but the valid flag will not be cleared. It is assumed that the load_cache routine will properly deal with being called with a valid cache.
.PP
If NETSNMP_CACHE_PRELOAD is set when a the cache handler is created, the cache load routine will be called immediately.
.PP
If NETSNMP_CACHE_DONT_AUTO_RELEASE is set, the periodic callback that checks for expired caches will skip the cache. The cache will only be checked for expiration when a request triggers the cache handler. This is useful if the cache has it's own periodic callback to keep the cache fresh.
.PP
If NETSNMP_CACHE_AUTO_RELOAD is set, a timer will be set up to reload the cache when it expires. This is useful for keeping the cache fresh, even in the absence of incoming snmp requests.
.PP
Here are some suggestions for some common situations.
.PP
Cached File: If your table is based on a file that may periodically change, you can test the modification date to see if the file has changed since the last cache load. To get the cache helper to call the load function for every request, set the timeout to -1, which will cause the cache to always report that it is expired. This means that you will want to prevent the agent from flushing the cache when it has expired, and you will have to flush it manually if you detect that the file has changed. To accomplish this, set the following flags:
.PP
NETSNMP_CACHE_DONT_FREE_EXPIRED NETSNMP_CACHE_DONT_AUTO_RELEASE
.PP
Constant (periodic) reload: If you want the cache kept up to date regularly, even if no requests for the table are received, you can have your cache load routine called periodically. This is very useful if you need to monitor the data for changes (eg a \fILastChanged\fP object). You will need to prevent the agent from flushing the cache when it expires. Set the cache timeout to the frequency, in seconds, that you wish to reload your cache, and set the following flags:
.PP
NETSNMP_CACHE_DONT_FREE_EXPIRED NETSNMP_CACHE_DONT_AUTO_RELEASE NETSNMP_CACHE_AUTO_RELOAD 
.SH "Function Documentation"
.PP 
.SS "int netsnmp_cache_check_and_reload (netsnmp_cache * cache)"
.PP
Reload the cache if required. 
.PP
Definition at line 382 of file cache_handler.c.
.PP
References netsnmp_cache_check_expired().
.PP
Referenced by netsnmp_cache_helper_handler().
.SS "int netsnmp_cache_check_expired (netsnmp_cache * cache)"
.PP
Check if the cache timeout has passed. 
.PP
Sets and return the expired flag. 
.PP
Definition at line 367 of file cache_handler.c.
.PP
Referenced by netsnmp_cache_check_and_reload(), and release_cached_resources().
.SS "netsnmp_cache* netsnmp_cache_create (int timeout, NetsnmpCacheLoad * load_hook, NetsnmpCacheFree * free_hook, oid * rootoid, int rootoid_len)"
.PP
returns a cache 
.PP
Definition at line 139 of file cache_handler.c.
.PP
References snmp_log().
.PP
Referenced by netsnmp_get_cache_handler().
.SS "netsnmp_cache* netsnmp_cache_find_by_oid (oid * rootoid, int rootoid_len)"
.PP
find existing cache 
.PP
Definition at line 123 of file cache_handler.c.
.PP
References netsnmp_oid_equals().
.SS "netsnmp_cache* netsnmp_cache_get_head (void)"
.PP
get cache head 
.PP
Definition at line 115 of file cache_handler.c.
.SS "\fBnetsnmp_mib_handler\fP* netsnmp_cache_handler_get (netsnmp_cache * cache)"
.PP
returns a cache handler that can be injected into a given handler chain. 
.PP
Definition at line 244 of file cache_handler.c.
.PP
References netsnmp_mib_handler_s::flags, netsnmp_mib_handler_s::myvoid, netsnmp_cache_helper_handler(), netsnmp_cache_timer_start(), and netsnmp_create_handler().
.PP
Referenced by netsnmp_cache_handler_register(), and netsnmp_get_cache_handler().
.SS "int netsnmp_cache_handler_register (\fBnetsnmp_handler_registration\fP * reginfo, netsnmp_cache * cache)"
.PP
functionally the same as calling \fBnetsnmp_register_handler()\fP but also injects a cache handler at the same time for you. 
.PP
Definition at line 292 of file cache_handler.c.
.PP
References netsnmp_cache_handler_get(), netsnmp_inject_handler(), and netsnmp_register_handler().
.SS "int netsnmp_cache_helper_handler (\fBnetsnmp_mib_handler\fP * handler, \fBnetsnmp_handler_registration\fP * reginfo, \fBnetsnmp_agent_request_info\fP * reqinfo, \fBnetsnmp_request_info\fP * requests)"
.PP
Implements the cache handler. 
.PP
next handler called automatically - 'AUTO_NEXT'
.PP
next handler called automatically - 'AUTO_NEXT'
.PP
next handler called automatically - 'AUTO_NEXT' 
.PP
Definition at line 417 of file cache_handler.c.
.PP
References netsnmp_mib_handler_s::flags, netsnmp_handler_registration_s::handlerName, netsnmp_agent_request_info_s::mode, netsnmp_mib_handler_s::myvoid, netsnmp_cache_check_and_reload(), netsnmp_cache_is_valid(), netsnmp_cache_reqinfo_insert(), netsnmp_set_all_requests_error(), netsnmp_handler_registration_s::rootoid, netsnmp_handler_registration_s::rootoid_len, and snmp_log().
.PP
Referenced by netsnmp_cache_handler_get().
.SS "int netsnmp_cache_is_valid (\fBnetsnmp_agent_request_info\fP * reqinfo, const char * name)"
.PP
Is the cache valid for a given request? 
.PP
Definition at line 399 of file cache_handler.c.
.PP
References netsnmp_cache_reqinfo_extract().
.PP
Referenced by netsnmp_cache_helper_handler(), and netsnmp_is_cache_valid().
.SS "netsnmp_cache* netsnmp_cache_reqinfo_extract (\fBnetsnmp_agent_request_info\fP * reqinfo, const char * name)"
.PP
Extract the cache information for a given request (PDU). 
.PP
Definition at line 347 of file cache_handler.c.
.PP
Referenced by netsnmp_cache_is_valid(), and netsnmp_extract_cache_info().
.SS "void netsnmp_cache_reqinfo_insert (netsnmp_cache * cache, \fBnetsnmp_agent_request_info\fP * reqinfo, const char * name)"
.PP
Insert the cache information for a given request (PDU). 
.PP
Definition at line 330 of file cache_handler.c.
.PP
References netsnmp_create_data_list().
.PP
Referenced by netsnmp_cache_helper_handler().
.SS "unsigned int netsnmp_cache_timer_start (netsnmp_cache * cache)"
.PP
starts the recurring cache_load callback 
.PP
Definition at line 194 of file cache_handler.c.
.PP
References snmp_alarm_register(), and snmp_log().
.PP
Referenced by netsnmp_cache_handler_get().
.SS "void netsnmp_cache_timer_stop (netsnmp_cache * cache)"
.PP
stops the recurring cache_load callback 
.PP
Definition at line 224 of file cache_handler.c.
.PP
References snmp_alarm_unregister(), and snmp_log().
.SS "netsnmp_cache* netsnmp_extract_cache_info (\fBnetsnmp_agent_request_info\fP * reqinfo)"
.PP
Extract the cache information for a given request (PDU). 
.PP
Definition at line 359 of file cache_handler.c.
.PP
References netsnmp_cache_reqinfo_extract().
.SS "\fBnetsnmp_mib_handler\fP* netsnmp_get_cache_handler (int timeout, NetsnmpCacheLoad * load_hook, NetsnmpCacheFree * free_hook, oid * rootoid, int rootoid_len)"
.PP
returns a cache handler that can be injected into a given handler chain. 
.PP
Definition at line 273 of file cache_handler.c.
.PP
References netsnmp_mib_handler_s::myvoid, netsnmp_cache_create(), and netsnmp_cache_handler_get().
.PP
Referenced by netsnmp_register_cache_handler().
.SS "int netsnmp_is_cache_valid (\fBnetsnmp_agent_request_info\fP * reqinfo)"
.PP
for backwards compat 
.PP
\fBnetsnmp_cache_is_valid()\fP is preferred. 
.PP
Definition at line 410 of file cache_handler.c.
.PP
References netsnmp_cache_is_valid().
.SS "int netsnmp_register_cache_handler (\fBnetsnmp_handler_registration\fP * reginfo, int timeout, NetsnmpCacheLoad * load_hook, NetsnmpCacheFree * free_hook)"
.PP
functionally the same as calling \fBnetsnmp_register_handler()\fP but also injects a cache handler at the same time for you. 
.PP
Definition at line 305 of file cache_handler.c.
.PP
References netsnmp_get_cache_handler(), netsnmp_inject_handler(), netsnmp_register_handler(), netsnmp_handler_registration_s::rootoid, and netsnmp_handler_registration_s::rootoid_len.
.SS "void release_cached_resources (unsigned int regNo, void * clientargs)"
.PP
run regularly to automatically release cached resources. 
.PP
xxx - method to prevent cache from expiring while a request is being processed (e.g. delegated request). proposal: set a flag, which would be cleared when request finished (which could be acomplished by a dummy data list item in agent req info & custom free function). 
.PP
Definition at line 574 of file cache_handler.c.
.PP
References netsnmp_cache_check_expired(), and snmp_alarm_register().
